package com.apes.fn.ssm.repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @description: 动态条件查询备货配置信息
 * @author: wsl
 * @time: 2022/7/01 10:21
 */
@Component
public class StockConfigDao extends ApesDao {
    /**
     * 批量查询备货配置
     * @param param
     * @return
     */
    public JSONArray queryStockConfig(JSONObject param) {
        JSONObject request = new JSONObject();
        StringBuffer sqlBuffer = new StringBuffer(" SELECT S.ID, S.VERSION ,S.LOCATION_ID, S.FITTING_MESSAGE_ID, S.SKU_COVER, S.SKU_MAX, S.SKU_MIN, S.QTY_MAX, S.QTY_MIN, S.QTY_GRADED, S.VIP_QTY, S.VALID,\n" +
                "        C.NAME LOCATION_NAME,F.NAME FITTING_NAME\n" +
                "      FROM MAS_STOCK S, MAS_LOCATION C, MAS_FITTING_MESSAGE F\n" +
                "      WHERE S.LOCATION_ID = C.ID AND S.FITTING_MESSAGE_ID = F.ID");

        if (Objects.nonNull(param.getJSONObject("location")) && param.getJSONObject("location").size() >0) {
            JSONObject json = param.getJSONObject("location");
            sqlBuffer.append(" AND S.LOCATION_ID = '" + json.getString("id") + "'");
        }if (Objects.nonNull(param.getJSONObject("fittingName")) && param.getJSONObject("fittingName").size() >0) {
            JSONObject json1 = param.getJSONObject("fittingName");
            sqlBuffer.append(" AND S.FITTING_MESSAGE_ID = " + json1.getString("id"));
        }if (Objects.nonNull(param.get("valid"))) {
            Boolean b = Boolean.valueOf(param.get("valid").toString());
            String array = b ? "1" : "0";
            sqlBuffer.append(" AND S.VALID = " + array);
        }
        String sql = sqlBuffer.toString();
        return toJSONArray(find(sql, request));
    }

    /**
     * 根据仓库和测算配件查询备货配置
     * @param locationArray
     * @param fittingMessageItems
     * @return
     */
    public List<Map> findByLocationId(JSONArray locationArray, JSONArray fittingMessageItems){
        StringBuffer sqlBuffer = new StringBuffer("SELECT A.ID,A.LOCATION_ID,A.FITTING_MESSAGE_ID," +
                "(SELECT B.NAME FROM MAS_LOCATION B WHERE A.LOCATION_ID = B.ID) LOCATION_NAME,\n" +
                "(SELECT C.NAME FROM MAS_FITTING_MESSAGE C WHERE A.FITTING_MESSAGE_ID = C.ID) FITTINGMESSAGE_NAME,\n" +
                "A.SKU_COVER,A.SKU_MAX,A.SKU_MIN,A.QTY_MAX,A.QTY_MIN,A.QTY_GRADED,A.VIP_QTY FROM MAS_STOCK A WHERE A.LOCATION_ID IN (");
        getQueryString(sqlBuffer, locationArray);
        sqlBuffer.append(" AND A.FITTING_MESSAGE_ID IN(");
        getQueryString(sqlBuffer, fittingMessageItems);
        return find(sqlBuffer.toString());
    }

    /**
     * 获取查选条件字符串
     * @param sql sql 语句
     * @param array 条件数组
     * @return 字符串
     */
    private StringBuffer getQueryString(StringBuffer sql, JSONArray array) {
        for (int i = 0; i < array.size(); i++) {
            JSONObject json = array.getJSONObject(i);
            if (i == 0) {
                sql.append("'").append(json.getString("id")).append("'");
            } else {
                sql.append(",'").append(json.getString("id")).append("'");
            }
        }
        sql.append(")");

        return sql;
    }
}
